package com.jinzhi.jzapi.common.controller;

import com.jinzhi.jzapi.common.pojo.CerUtilsVo;
import com.jinzhi.jzapi.common.pojo.SchoolCerUtilsVO;
import com.jinzhi.jzapi.utils.CerSynthesisUtil;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.TrainingCerVo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.CertificateUtil;
import com.jinzhi.jzweb.utils.ValuesUtil;
import com.jinzhi.oss.service.OssService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Component
public class SchoolApittController {

    @Autowired
    private SchoolCertificatesService schoolCertificatesService;

    //幼儿照护
    @Async
    public void yezhzs(List<SchoolCerUtilsVO> schoolCerUtilsVOS, List<SchoolCertificatesDO> schoolCertificatesDOList){
        // 实现run方法
        for (int i = 0; i < schoolCerUtilsVOS.size(); i++) {
            //证书图片
            String cerImgUrl = null;
            String cerImgUrlNoSale = null;
            if (schoolCerUtilsVOS.get(i).getCertificatesType() == 1) {
                try {
                    //参数说明：合成类型（0带公章，1不带公章），背景图，学员姓名，学员图像， 身份证号码，证书编码，考核期数，培训章节数（学时），培训年份，培训月份，培训日期，获证年份，获证年份，获证年份，电子公章
                    cerImgUrl = CerSynthesisUtil.yezhkhzdpb(0,schoolCerUtilsVOS.get(i).getBackgroundPath(),schoolCerUtilsVOS.get(i).getOrgName(),schoolCerUtilsVOS.get(i).getCode(),schoolCerUtilsVOS.get(i).getCerYear(),schoolCerUtilsVOS.get(i).getCerMath(),schoolCerUtilsVOS.get(i).getElectronicSeal());
                    cerImgUrlNoSale = CerSynthesisUtil.yezhkhzdpb(1,schoolCerUtilsVOS.get(i).getBackgroundPath(),schoolCerUtilsVOS.get(i).getOrgName(),schoolCerUtilsVOS.get(i).getCode(),schoolCerUtilsVOS.get(i).getCerYear(),schoolCerUtilsVOS.get(i).getCerMath(),schoolCerUtilsVOS.get(i).getElectronicSeal());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            //转换一下图片路径，将反斜杠替换掉
            String url = cerImgUrl.replace(ValuesUtil.localUrl, ValuesUtil.returnUrl);
            String imgUrl1 = url.replace("\\", "/");
            schoolCertificatesDOList.get(i).setImgUrl(imgUrl1);//证书图片
            if (schoolCerUtilsVOS.get(i).getElectronicSealType() == 1) {
                //转换一下图片路径，将反斜杠替换掉
                String url1 = cerImgUrlNoSale.replace(ValuesUtil.localUrl, ValuesUtil.returnUrl);
                String imgUrl2 = url1.replace("\\", "/");
                schoolCertificatesDOList.get(i).setImgUrlNosale(imgUrl2);//不带电子公章的证书图片链接
            }

        }
        schoolCertificatesService.updateBatchById(schoolCertificatesDOList,schoolCertificatesDOList.size());
        System.out.println("修改成功------------------------------------------------------------------------------------");
    }

    //产后恢复
    @Async
    public void chhfzs(List<SchoolCerUtilsVO> schoolCerUtilsVOS, List<SchoolCertificatesDO> schoolCertificatesDOList){
        // 实现run方法
        for (int i = 0; i < schoolCerUtilsVOS.size(); i++) {
            //证书图片
            String cerImgUrl = null;
            String cerImgUrlNoSale = null;
            if (schoolCerUtilsVOS.get(i).getCertificatesType() == 1) {
                try {
                    //参数说明：合成类型（0带公章，1不带公章），背景图，学员姓名，学员图像， 身份证号码，证书编码，考核期数，培训章节数（学时），培训年份，培训月份，培训日期，获证年份，获证年份，获证年份，电子公章
                    cerImgUrl = CerSynthesisUtil.chhfkhzdpb(0,schoolCerUtilsVOS.get(i).getBackgroundPath(),schoolCerUtilsVOS.get(i).getOrgName(),schoolCerUtilsVOS.get(i).getCode(),schoolCerUtilsVOS.get(i).getCerYear(),schoolCerUtilsVOS.get(i).getCerMath(),schoolCerUtilsVOS.get(i).getElectronicSeal());
                    cerImgUrlNoSale = CerSynthesisUtil.chhfkhzdpb(1,schoolCerUtilsVOS.get(i).getBackgroundPath(),schoolCerUtilsVOS.get(i).getOrgName(),schoolCerUtilsVOS.get(i).getCode(),schoolCerUtilsVOS.get(i).getCerYear(),schoolCerUtilsVOS.get(i).getCerMath(),schoolCerUtilsVOS.get(i).getElectronicSeal());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            //转换一下图片路径，将反斜杠替换掉
            String url = cerImgUrl.replace(ValuesUtil.localUrl, ValuesUtil.returnUrl);
            String imgUrl1 = url.replace("\\", "/");
            schoolCertificatesDOList.get(i).setImgUrl(imgUrl1);//证书图片
            if (schoolCerUtilsVOS.get(i).getElectronicSealType() == 1) {
                //转换一下图片路径，将反斜杠替换掉
                String url1 = cerImgUrlNoSale.replace(ValuesUtil.localUrl, ValuesUtil.returnUrl);
                String imgUrl2 = url1.replace("\\", "/");
                schoolCertificatesDOList.get(i).setImgUrlNosale(imgUrl2);//不带电子公章的证书图片链接
            }

        }
        schoolCertificatesService.updateBatchById(schoolCertificatesDOList,schoolCertificatesDOList.size());
        System.out.println("修改成功------------------------------------------------------------------------------------");
    }

}
